package org.gongliang.jsmile.authc.sys.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.gongliang.jsmile.authc.sys.entity.SysDictionary;
import org.gongliang.jsmile.authc.sys.service.SysDictionaryService;
import org.gongliang.jsmile.common.base.BaseController;
import org.gongliang.jsmile.common.base.JSmileResult;
import org.gongliang.jsmile.common.constants.KConstant;
import org.gongliang.jsmile.common.enums.KCache;
import org.gongliang.jsmile.common.enums.KException;
import org.gongliang.jsmile.common.kit.CacheKit;
import org.gongliang.jsmile.common.kit.web.ShiroKit;
import org.gongliang.jsmile.common.kit.web.WebKit;
import org.gongliang.jsmile.config.log.info.KLog;
import org.gongliang.jsmile.config.log.info.LogType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
* @desc   控制器
* @author 龚亮
* @version 1.0
* @date 2018年08月25日 04:57:24
*/
@Controller
@RequestMapping("/sys_dictionary")
@Api(tags = "字典管理")
public class SysDictionaryController extends BaseController {

    @Autowired
    private SysDictionaryService sysDictionaryService;
    
	@GetMapping("/main")
	public String main() {
		return "sys/Dictionary/dict";
	}
	@GetMapping("/add")
	public String add() {
		return "sys/Dictionary/add";
	}
	@GetMapping("/edit")
	public String edit() {
		return "sys/Dictionary/edit";
	}

	@ResponseBody
	@RequestMapping("/findDictByCode")
	@ApiOperation(value = "根据字典编码获取字典列表")
	@KLog(title = "字典管理->根据字典编码获取字典列表", type = LogType.QUERY)
	public JSmileResult findDictByCode(SysDictionary dict) {
		List<SysDictionary> list = sysDictionaryService.findDictByCode(dict);
		JSmileResult result = JSmileResult.success();
		result.setResData(list);
		return result;
	}

	/**
	 * 查询所有分页 返回 Lit<Map>
	 */
	@ResponseBody
	@ApiOperation(value = "分页获取字典列表")
	@RequestMapping("/findAllByPage")
	@KLog(title = "日志管理->分页获取字典列表", type = LogType.QUERY)
	public JSmileResult findAllMapByPage(HttpServletRequest request) {
		Map<String, Object> reqMap = WebKit.getParams(request);
		WebKit.setPage(WebKit.toPageIndex(reqMap), WebKit.toPageSize(reqMap));
		List<SysDictionary> list = sysDictionaryService.findAll(reqMap);
		PageInfo<SysDictionary> pages = new PageInfo<SysDictionary>(list);
		JSmileResult result = JSmileResult.success();
		result.setResData(pages.getList(), pages.getTotal());
		return result;
	}
	
	@ResponseBody
	@RequestMapping("/findAll")
	@ApiOperation(value = "获取字典列表", notes = "获取字典列表(不分页)")
	@KLog(title = "字典管理->获取字典列表", type = LogType.QUERY)
	public JSmileResult findAll(HttpServletRequest request) {
		Map<String, Object> reqMap = WebKit.getParams(request);
		List<SysDictionary> list = sysDictionaryService.findAll(reqMap);
		JSmileResult result = JSmileResult.success();
		result.setResData(list);
		return result;
	}


	@KLog(title = "字典管理->添加数据字典", type = LogType.INSERT)
	@RequiresPermissions("sys:dict:add")
	@ResponseBody
	@PostMapping("/save")
	public JSmileResult save(SysDictionary dictionary) {
		dictionary.setCreateDate(new Date());
		dictionary.setCreator(ShiroKit.getUserId());
		CacheKit.remove(KCache.sysDictCache, KConstant.SYS_DICT_CACHE);
		return sysDictionaryService.saveDict(dictionary) ? JSmileResult.success("添加数据字典信息成功!") : JSmileResult.error("添加数据字典信息失败!");
	}

	@RequiresPermissions("sys:dict:update")
	@ResponseBody
	@PostMapping("/update")
	@KLog(title = "字典管理->修改数据字典", type = LogType.UPDATE)
	public JSmileResult update(SysDictionary dictionary) {
		dictionary.setUpdateDate(new Date());
		dictionary.setCreator(ShiroKit.getUserId());
		return sysDictionaryService.updateById(dictionary) ? JSmileResult.success("更新字典信息成功!") : JSmileResult.error("更新字典信息失败!");
	}

	/**
	 * 根据id删除
	 */
	@KLog(title = "字典管理->删除数据字典", type = LogType.DELETE)
	@RequiresPermissions("sys:dict:delete")
	@ResponseBody
	@PostMapping("/deleteByIds")
	public JSmileResult deleteByIds(@RequestParam(value = "ids[]", required = true) String[] ids) {
		if (ids == null || ids.length == 0) {
			return JSmileResult.error(KException.IDS_NOT_NULL);
		}
		CacheKit.remove(KCache.sysDictCache, KConstant.SYS_DICT_CACHE);
		return sysDictionaryService.deleteDicts(ids) ? JSmileResult.success("删除字典项成功!") : JSmileResult.error("删除字典项失败!");
	}
	
	/**
	 * desc:根据字典编码获取最大的dictValue和sort
	 * 
	 * @author 龚亮
	 * @throws Exception
	 */
	@RequestMapping("/getMaxDict")
	@ResponseBody
	public JSmileResult getMaxDict(SysDictionary dictionary) throws Exception {
		JSmileResult result=JSmileResult.success();
		SysDictionary dict = sysDictionaryService.getMaxDict(dictionary);
		result.setResExtraObj(dict);
		return result;
	}
}
